﻿using System;
using System.Collections.Generic;

namespace tiny_compiler
{
	public static class CompilerUtility
	{
		public static int Offset(this IEnumerable<IOperator> _operators, string _name)
		{
			int result = 0;
			foreach (var op in _operators)
			{
				if (op.Name == _name)
					return result;
				result += op.Length;
			}

			throw new ArgumentException(string.Format("Value '{0}' not found", _name));
		}

		public static IOperator Operator(this IEnumerable<IOperator> _operators, string _name)
		{
			foreach (var op in _operators)
			{
				if (op.Name == _name)
					return op;
			}

			throw new ArgumentException(string.Format("Value '{0}' not found", _name));
		}

		public static string ConvertNumber(int _value)
		{
			return _value.ToString(Compiler.NumberFormat);
		}

		public static string ConvertLength(int _value)
		{
			return new string((char)(_value + 'a'), 1);
		}
	}
}
